
/* ---------------------------------------------------------------------- */
/* CTDocumento											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='CTDocumento_FK_1')
	ALTER TABLE [CTDocumento] DROP CONSTRAINT [CTDocumento_FK_1];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'CTDocumento')
BEGIN
	 DECLARE @reftable_1 nvarchar(60), @constraintname_1 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'CTDocumento'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_1, @constraintname_1
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_1+' drop constraint '+@constraintname_1)
	   FETCH NEXT from refcursor into @reftable_1, @constraintname_1
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [CTDocumento]
END


CREATE TABLE [CTDocumento]
(
	[id] INT  NOT NULL IDENTITY,
	[usuario] INT  NULL,
	[fechaCambio] DATETIME  NOT NULL,
	[idDocumentoPadre] INT  NULL,
	[fechaBaja] DATETIME  NULL,
	[fechaAlta] DATETIME  NOT NULL,
	CONSTRAINT [CTDocumento_PK] PRIMARY KEY ([id])
);

BEGIN
ALTER TABLE [CTDocumento] ADD CONSTRAINT [CTDocumento_FK_1] FOREIGN KEY ([idDocumentoPadre]) REFERENCES [CTDocumento] ([id])
END
;

/* ---------------------------------------------------------------------- */
/* CTLibro											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='CTLibro_FK_1')
	ALTER TABLE [CTLibro] DROP CONSTRAINT [CTLibro_FK_1];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='CTLibro_FK_2')
	ALTER TABLE [CTLibro] DROP CONSTRAINT [CTLibro_FK_2];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='CTLibro_FK_3')
	ALTER TABLE [CTLibro] DROP CONSTRAINT [CTLibro_FK_3];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='CTLibro_FK_4')
	ALTER TABLE [CTLibro] DROP CONSTRAINT [CTLibro_FK_4];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'CTLibro')
BEGIN
	 DECLARE @reftable_2 nvarchar(60), @constraintname_2 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'CTLibro'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_2, @constraintname_2
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_2+' drop constraint '+@constraintname_2)
	   FETCH NEXT from refcursor into @reftable_2, @constraintname_2
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [CTLibro]
END


CREATE TABLE [CTLibro]
(
	[idLibro] INT  NOT NULL IDENTITY,
	[ubicacion] VARCHAR(200)  NULL,
	[signatura_clase] VARCHAR(200)  NULL,
	[signatura_libristica] VARCHAR(200)  NULL,
	[volumen] VARCHAR(200)  NULL,
	[inventarioLibro] VARCHAR(100)  NOT NULL,
	[idDocumento] INT  NOT NULL,
	[idSucursal] INT  NOT NULL,
	[idTipoObra] INT  NULL,
	[fechaIngreso] DATETIME  NOT NULL,
	[fechaBaja] DATETIME  NULL,
	[estadolibro] INT  NOT NULL,
	[fechaCambio] DATETIME  NOT NULL,
	[usuario] INT  NOT NULL,
	CONSTRAINT [CTLibro_PK] PRIMARY KEY ([idLibro])
);

BEGIN
ALTER TABLE [CTLibro] ADD CONSTRAINT [CTLibro_FK_1] FOREIGN KEY ([idDocumento]) REFERENCES [CTDocumento] ([id]) ON DELETE CASCADE
END
;

BEGIN
ALTER TABLE [CTLibro] ADD CONSTRAINT [CTLibro_FK_2] FOREIGN KEY ([idSucursal]) REFERENCES [BBSucursal] ([idSucursal]) ON DELETE CASCADE
END
;

BEGIN
ALTER TABLE [CTLibro] ADD CONSTRAINT [CTLibro_FK_3] FOREIGN KEY ([idTipoObra]) REFERENCES [CTTipoObra] ([idTipoObra])
END
;

BEGIN
ALTER TABLE [CTLibro] ADD CONSTRAINT [CTLibro_FK_4] FOREIGN KEY ([estadolibro]) REFERENCES [CTEstadoLibro] ([idEstado])
END
;

/* ---------------------------------------------------------------------- */
/* CTDocumentoCaracteristica											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='CTDocumentoCaracteristica_FK_1')
	ALTER TABLE [CTDocumentoCaracteristica] DROP CONSTRAINT [CTDocumentoCaracteristica_FK_1];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='CTDocumentoCaracteristica_FK_2')
	ALTER TABLE [CTDocumentoCaracteristica] DROP CONSTRAINT [CTDocumentoCaracteristica_FK_2];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='CTDocumentoCaracteristica_FK_3')
	ALTER TABLE [CTDocumentoCaracteristica] DROP CONSTRAINT [CTDocumentoCaracteristica_FK_3];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'CTDocumentoCaracteristica')
BEGIN
	 DECLARE @reftable_3 nvarchar(60), @constraintname_3 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'CTDocumentoCaracteristica'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_3, @constraintname_3
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_3+' drop constraint '+@constraintname_3)
	   FETCH NEXT from refcursor into @reftable_3, @constraintname_3
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [CTDocumentoCaracteristica]
END


CREATE TABLE [CTDocumentoCaracteristica]
(
	[id] INT  NOT NULL IDENTITY,
	[idDocumento] INT  NOT NULL,
	[idCaracteristica] INT  NOT NULL,
	[idIteracion] INT  NULL,
	[valorCaracteristica] VARCHAR(MAX)  NULL,
	[ValorTipificacion] VARCHAR(255)  NULL,
	[usuario] INT  NULL,
	[fechaCambio] DATETIME  NOT NULL,
	CONSTRAINT [CTDocumentoCaracteristica_PK] PRIMARY KEY ([id])
);

BEGIN
ALTER TABLE [CTDocumentoCaracteristica] ADD CONSTRAINT [CTDocumentoCaracteristica_FK_1] FOREIGN KEY ([idDocumento]) REFERENCES [CTDocumento] ([id])
END
;

BEGIN
ALTER TABLE [CTDocumentoCaracteristica] ADD CONSTRAINT [CTDocumentoCaracteristica_FK_2] FOREIGN KEY ([idCaracteristica]) REFERENCES [CTCaracteristica] ([idCaracteristica])
END
;

BEGIN
ALTER TABLE [CTDocumentoCaracteristica] ADD CONSTRAINT [CTDocumentoCaracteristica_FK_3] FOREIGN KEY ([ValorTipificacion]) REFERENCES [CTValores] ([idValor]) ON DELETE CASCADE
END
;

/* ---------------------------------------------------------------------- */
/* CTCaracteristica											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='CTCaracteristica_FK_1')
	ALTER TABLE [CTCaracteristica] DROP CONSTRAINT [CTCaracteristica_FK_1];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='CTCaracteristica_FK_2')
	ALTER TABLE [CTCaracteristica] DROP CONSTRAINT [CTCaracteristica_FK_2];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'CTCaracteristica')
BEGIN
	 DECLARE @reftable_4 nvarchar(60), @constraintname_4 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'CTCaracteristica'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_4, @constraintname_4
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_4+' drop constraint '+@constraintname_4)
	   FETCH NEXT from refcursor into @reftable_4, @constraintname_4
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [CTCaracteristica]
END


CREATE TABLE [CTCaracteristica]
(
	[idCaracteristica] INT  NOT NULL IDENTITY,
	[descripcionCaracteristica] VARCHAR(100)  NOT NULL,
	[longitudCaracteristica] SMALLINT  NOT NULL,
	[idTipoDatoCaracteristica] VARCHAR(255)  NOT NULL,
	[requeridaCaracteristica] INT  NOT NULL,
	[repetibleCaracteristica] INT  NOT NULL,
	[visibleCaracteristica] INT  NOT NULL,
	[idTipificacion] VARCHAR(255)  NOT NULL,
	[usuario] INT  NULL,
	[porDefecto] INT  NULL,
	[codigoCaracteristica] INT  NULL,
	CONSTRAINT [CTCaracteristica_PK] PRIMARY KEY ([idCaracteristica])
);

BEGIN
ALTER TABLE [CTCaracteristica] ADD CONSTRAINT [CTCaracteristica_FK_1] FOREIGN KEY ([idTipoDatoCaracteristica]) REFERENCES [CTTipoDato] ([idTipoDato]) ON DELETE CASCADE
END
;

BEGIN
ALTER TABLE [CTCaracteristica] ADD CONSTRAINT [CTCaracteristica_FK_2] FOREIGN KEY ([idTipificacion]) REFERENCES [CTTipificacion] ([idTipificacion]) ON DELETE CASCADE
END
;

/* ---------------------------------------------------------------------- */
/* CTCaracteristicaPadre											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='CTCaracteristicaPadre_FK_1')
	ALTER TABLE [CTCaracteristicaPadre] DROP CONSTRAINT [CTCaracteristicaPadre_FK_1];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='CTCaracteristicaPadre_FK_2')
	ALTER TABLE [CTCaracteristicaPadre] DROP CONSTRAINT [CTCaracteristicaPadre_FK_2];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'CTCaracteristicaPadre')
BEGIN
	 DECLARE @reftable_5 nvarchar(60), @constraintname_5 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'CTCaracteristicaPadre'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_5, @constraintname_5
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_5+' drop constraint '+@constraintname_5)
	   FETCH NEXT from refcursor into @reftable_5, @constraintname_5
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [CTCaracteristicaPadre]
END


CREATE TABLE [CTCaracteristicaPadre]
(
	[idCaracteristicaPadre] INT  NOT NULL,
	[idCaracteristica] INT  NOT NULL,
	[OrdenHijo] INT  NOT NULL,
	[usuario] INT  NOT NULL,
	[CaracterBusqueda] VARCHAR(50)  NULL,
	CONSTRAINT [CTCaracteristicaPadre_PK] PRIMARY KEY ([idCaracteristicaPadre],[idCaracteristica])
);

BEGIN
ALTER TABLE [CTCaracteristicaPadre] ADD CONSTRAINT [CTCaracteristicaPadre_FK_1] FOREIGN KEY ([idCaracteristicaPadre]) REFERENCES [CTCaracteristica] ([idCaracteristica]) ON DELETE CASCADE
END
;

BEGIN
ALTER TABLE [CTCaracteristicaPadre] ADD CONSTRAINT [CTCaracteristicaPadre_FK_2] FOREIGN KEY ([idCaracteristica]) REFERENCES [CTCaracteristica] ([idCaracteristica]) ON DELETE CASCADE
END
;

/* ---------------------------------------------------------------------- */
/* CTTipoDato											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'CTTipoDato')
BEGIN
	 DECLARE @reftable_6 nvarchar(60), @constraintname_6 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'CTTipoDato'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_6, @constraintname_6
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_6+' drop constraint '+@constraintname_6)
	   FETCH NEXT from refcursor into @reftable_6, @constraintname_6
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [CTTipoDato]
END


CREATE TABLE [CTTipoDato]
(
	[idTipoDato] INT  NOT NULL IDENTITY,
	[nombreTipoDato] VARCHAR(50)  NOT NULL,
	[formatoTipoDato] VARCHAR(50)  NULL,
	[usuario] INT  NULL,
	CONSTRAINT [CTTipoDato_PK] PRIMARY KEY ([idTipoDato])
);

/* ---------------------------------------------------------------------- */
/* CTTipificacion											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'CTTipificacion')
BEGIN
	 DECLARE @reftable_7 nvarchar(60), @constraintname_7 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'CTTipificacion'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_7, @constraintname_7
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_7+' drop constraint '+@constraintname_7)
	   FETCH NEXT from refcursor into @reftable_7, @constraintname_7
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [CTTipificacion]
END


CREATE TABLE [CTTipificacion]
(
	[idTipificacion] INT  NOT NULL IDENTITY,
	[nombreTipificacion] VARCHAR(30)  NOT NULL,
	[usuario] INT  NULL,
	CONSTRAINT [CTTipificacion_PK] PRIMARY KEY ([idTipificacion])
);

/* ---------------------------------------------------------------------- */
/* CTValores											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'CTValores')
BEGIN
	 DECLARE @reftable_8 nvarchar(60), @constraintname_8 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'CTValores'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_8, @constraintname_8
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_8+' drop constraint '+@constraintname_8)
	   FETCH NEXT from refcursor into @reftable_8, @constraintname_8
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [CTValores]
END


CREATE TABLE [CTValores]
(
	[idValor] INT  NOT NULL IDENTITY,
	[codigoValor] VARCHAR(10)  NULL,
	[descripcionValor] VARCHAR(50)  NULL,
	[usuario] INT  NULL,
	CONSTRAINT [CTValores_PK] PRIMARY KEY ([idValor])
);

/* ---------------------------------------------------------------------- */
/* CTTipoObra											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'CTTipoObra')
BEGIN
	 DECLARE @reftable_9 nvarchar(60), @constraintname_9 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'CTTipoObra'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_9, @constraintname_9
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_9+' drop constraint '+@constraintname_9)
	   FETCH NEXT from refcursor into @reftable_9, @constraintname_9
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [CTTipoObra]
END


CREATE TABLE [CTTipoObra]
(
	[idTipoObra] INT  NOT NULL IDENTITY,
	[tipoObra] VARCHAR(30)  NOT NULL,
	CONSTRAINT [CTTipoObra_PK] PRIMARY KEY ([idTipoObra])
);

/* ---------------------------------------------------------------------- */
/* CTEstadoLibro											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'CTEstadoLibro')
BEGIN
	 DECLARE @reftable_10 nvarchar(60), @constraintname_10 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'CTEstadoLibro'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_10, @constraintname_10
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_10+' drop constraint '+@constraintname_10)
	   FETCH NEXT from refcursor into @reftable_10, @constraintname_10
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [CTEstadoLibro]
END


CREATE TABLE [CTEstadoLibro]
(
	[idEstado] INT  NOT NULL IDENTITY,
	[Estado] VARCHAR(20)  NOT NULL,
	[Entregable] INT  NOT NULL,
	[Usuario] INT  NULL,
	CONSTRAINT [CTEstadoLibro_PK] PRIMARY KEY ([idEstado])
);

/* ---------------------------------------------------------------------- */
/* BBSucursal											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'BBSucursal')
BEGIN
	 DECLARE @reftable_11 nvarchar(60), @constraintname_11 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'BBSucursal'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_11, @constraintname_11
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_11+' drop constraint '+@constraintname_11)
	   FETCH NEXT from refcursor into @reftable_11, @constraintname_11
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [BBSucursal]
END


CREATE TABLE [BBSucursal]
(
	[idSucursal] INT  NOT NULL IDENTITY,
	[nombreSucursal] VARCHAR(50)  NOT NULL,
	[usuario] INT  NULL,
	[codigoSucursal] VARCHAR(50)  NULL,
	CONSTRAINT [BBSucursal_PK] PRIMARY KEY ([idSucursal])
);

/* ---------------------------------------------------------------------- */
/* CTValoresTipificacion											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='CTValoresTipificacion_FK_1')
	ALTER TABLE [CTValoresTipificacion] DROP CONSTRAINT [CTValoresTipificacion_FK_1];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='CTValoresTipificacion_FK_2')
	ALTER TABLE [CTValoresTipificacion] DROP CONSTRAINT [CTValoresTipificacion_FK_2];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'CTValoresTipificacion')
BEGIN
	 DECLARE @reftable_12 nvarchar(60), @constraintname_12 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'CTValoresTipificacion'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_12, @constraintname_12
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_12+' drop constraint '+@constraintname_12)
	   FETCH NEXT from refcursor into @reftable_12, @constraintname_12
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [CTValoresTipificacion]
END


CREATE TABLE [CTValoresTipificacion]
(
	[idTipificacion] INT  NOT NULL,
	[idValor] INT  NOT NULL,
	[usuario] INT  NULL,
	CONSTRAINT [CTValoresTipificacion_PK] PRIMARY KEY ([idTipificacion],[idValor])
);

BEGIN
ALTER TABLE [CTValoresTipificacion] ADD CONSTRAINT [CTValoresTipificacion_FK_1] FOREIGN KEY ([idTipificacion]) REFERENCES [CTTipificacion] ([idTipificacion]) ON DELETE CASCADE
END
;

BEGIN
ALTER TABLE [CTValoresTipificacion] ADD CONSTRAINT [CTValoresTipificacion_FK_2] FOREIGN KEY ([idValor]) REFERENCES [CTValores] ([idValor]) ON DELETE CASCADE
END
;

/* ---------------------------------------------------------------------- */
/* CTDocumentoValoracion											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='CTDocumentoValoracion_FK_1')
	ALTER TABLE [CTDocumentoValoracion] DROP CONSTRAINT [CTDocumentoValoracion_FK_1];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'CTDocumentoValoracion')
BEGIN
	 DECLARE @reftable_13 nvarchar(60), @constraintname_13 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'CTDocumentoValoracion'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_13, @constraintname_13
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_13+' drop constraint '+@constraintname_13)
	   FETCH NEXT from refcursor into @reftable_13, @constraintname_13
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [CTDocumentoValoracion]
END


CREATE TABLE [CTDocumentoValoracion]
(
	[idValoracion] INT  NOT NULL IDENTITY,
	[idDocumento] VARCHAR(255)  NOT NULL,
	[IdPersona] INT  NOT NULL,
	[nombrePersona] VARCHAR(200)  NULL,
	[valor] INT  NULL,
	[fecha] DATETIME  NOT NULL,
	CONSTRAINT [CTDocumentoValoracion_PK] PRIMARY KEY ([idValoracion])
);

BEGIN
ALTER TABLE [CTDocumentoValoracion] ADD CONSTRAINT [CTDocumentoValoracion_FK_1] FOREIGN KEY ([idDocumento]) REFERENCES [CTDocumento] ([id]) ON DELETE CASCADE
END
;

/* ---------------------------------------------------------------------- */
/* CTDocumentoComentario											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='CTDocumentoComentario_FK_1')
	ALTER TABLE [CTDocumentoComentario] DROP CONSTRAINT [CTDocumentoComentario_FK_1];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'CTDocumentoComentario')
BEGIN
	 DECLARE @reftable_14 nvarchar(60), @constraintname_14 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'CTDocumentoComentario'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_14, @constraintname_14
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_14+' drop constraint '+@constraintname_14)
	   FETCH NEXT from refcursor into @reftable_14, @constraintname_14
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [CTDocumentoComentario]
END


CREATE TABLE [CTDocumentoComentario]
(
	[idComentario] INT  NOT NULL IDENTITY,
	[idDocumento] VARCHAR(255)  NOT NULL,
	[IdPersona] INT  NOT NULL,
	[nombrePersona] VARCHAR(200)  NULL,
	[comentario] VARCHAR(MAX)  NULL,
	[fecha] DATETIME  NOT NULL,
	[fechaBaja] DATETIME  NULL,
	CONSTRAINT [CTDocumentoComentario_PK] PRIMARY KEY ([idComentario])
);

BEGIN
ALTER TABLE [CTDocumentoComentario] ADD CONSTRAINT [CTDocumentoComentario_FK_1] FOREIGN KEY ([idDocumento]) REFERENCES [CTDocumento] ([id]) ON DELETE CASCADE
END
;

/* ---------------------------------------------------------------------- */
/* CTGrupo											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='CTGrupo_FK_1')
	ALTER TABLE [CTGrupo] DROP CONSTRAINT [CTGrupo_FK_1];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'CTGrupo')
BEGIN
	 DECLARE @reftable_15 nvarchar(60), @constraintname_15 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'CTGrupo'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_15, @constraintname_15
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_15+' drop constraint '+@constraintname_15)
	   FETCH NEXT from refcursor into @reftable_15, @constraintname_15
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [CTGrupo]
END


CREATE TABLE [CTGrupo]
(
	[idGrupo] INT  NOT NULL IDENTITY,
	[ordenGrupo] INT  NOT NULL,
	[descripcionGrupo] VARCHAR(50)  NOT NULL,
	[visibleGrupo] INT  NOT NULL,
	[IdModelo] VARCHAR(255)  NULL,
	[Usuario] INT  NULL,
	[TipoGrupo] TINYINT  NOT NULL,
	CONSTRAINT [CTGrupo_PK] PRIMARY KEY ([idGrupo])
);

BEGIN
ALTER TABLE [CTGrupo] ADD CONSTRAINT [CTGrupo_FK_1] FOREIGN KEY ([IdModelo]) REFERENCES [CTModelo] ([IdModelo]) ON DELETE CASCADE
END
;

/* ---------------------------------------------------------------------- */
/* CTModelo											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'CTModelo')
BEGIN
	 DECLARE @reftable_16 nvarchar(60), @constraintname_16 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'CTModelo'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_16, @constraintname_16
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_16+' drop constraint '+@constraintname_16)
	   FETCH NEXT from refcursor into @reftable_16, @constraintname_16
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [CTModelo]
END


CREATE TABLE [CTModelo]
(
	[IdModelo] INT  NOT NULL IDENTITY,
	[DescripcionModelo] VARCHAR(30)  NULL,
	[usuario] INT  NULL,
	CONSTRAINT [CTModelo_PK] PRIMARY KEY ([IdModelo])
);

/* ---------------------------------------------------------------------- */
/* CTGrupoOrdenamiento											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='CTGrupoOrdenamiento_FK_1')
	ALTER TABLE [CTGrupoOrdenamiento] DROP CONSTRAINT [CTGrupoOrdenamiento_FK_1];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='CTGrupoOrdenamiento_FK_2')
	ALTER TABLE [CTGrupoOrdenamiento] DROP CONSTRAINT [CTGrupoOrdenamiento_FK_2];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'CTGrupoOrdenamiento')
BEGIN
	 DECLARE @reftable_17 nvarchar(60), @constraintname_17 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'CTGrupoOrdenamiento'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_17, @constraintname_17
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_17+' drop constraint '+@constraintname_17)
	   FETCH NEXT from refcursor into @reftable_17, @constraintname_17
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [CTGrupoOrdenamiento]
END


CREATE TABLE [CTGrupoOrdenamiento]
(
	[idGrupo] INT  NOT NULL,
	[idCaracteristica] INT  NOT NULL,
	[ordenOrdenamiento] INT  NOT NULL,
	CONSTRAINT [CTGrupoOrdenamiento_PK] PRIMARY KEY ([idGrupo],[idCaracteristica],[ordenOrdenamiento])
);

BEGIN
ALTER TABLE [CTGrupoOrdenamiento] ADD CONSTRAINT [CTGrupoOrdenamiento_FK_1] FOREIGN KEY ([idGrupo]) REFERENCES [CTGrupo] ([idGrupo]) ON DELETE CASCADE
END
;

BEGIN
ALTER TABLE [CTGrupoOrdenamiento] ADD CONSTRAINT [CTGrupoOrdenamiento_FK_2] FOREIGN KEY ([idCaracteristica]) REFERENCES [CTCaracteristica] ([idCaracteristica]) ON DELETE CASCADE
END
;

/* ---------------------------------------------------------------------- */
/* CTGrupoCaracteristica											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='CTGrupoCaracteristica_FK_1')
	ALTER TABLE [CTGrupoCaracteristica] DROP CONSTRAINT [CTGrupoCaracteristica_FK_1];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='CTGrupoCaracteristica_FK_2')
	ALTER TABLE [CTGrupoCaracteristica] DROP CONSTRAINT [CTGrupoCaracteristica_FK_2];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'CTGrupoCaracteristica')
BEGIN
	 DECLARE @reftable_18 nvarchar(60), @constraintname_18 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'CTGrupoCaracteristica'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_18, @constraintname_18
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_18+' drop constraint '+@constraintname_18)
	   FETCH NEXT from refcursor into @reftable_18, @constraintname_18
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [CTGrupoCaracteristica]
END


CREATE TABLE [CTGrupoCaracteristica]
(
	[idGrupo] INT  NOT NULL,
	[idPadre] INT  NOT NULL,
	[OrdenPadre] INT  NULL,
	[OrdenHijo] INT  NULL,
	[usuario] INT  NULL,
	CONSTRAINT [CTGrupoCaracteristica_PK] PRIMARY KEY ([idGrupo],[idPadre])
);

BEGIN
ALTER TABLE [CTGrupoCaracteristica] ADD CONSTRAINT [CTGrupoCaracteristica_FK_1] FOREIGN KEY ([idGrupo]) REFERENCES [CTGrupo] ([idGrupo])
END
;

BEGIN
ALTER TABLE [CTGrupoCaracteristica] ADD CONSTRAINT [CTGrupoCaracteristica_FK_2] FOREIGN KEY ([idPadre]) REFERENCES [CTCaracteristica] ([idCaracteristica])
END
;

/* ---------------------------------------------------------------------- */
/* BBPrestamo											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='BBPrestamo_FK_1')
	ALTER TABLE [BBPrestamo] DROP CONSTRAINT [BBPrestamo_FK_1];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='BBPrestamo_FK_2')
	ALTER TABLE [BBPrestamo] DROP CONSTRAINT [BBPrestamo_FK_2];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='BBPrestamo_FK_3')
	ALTER TABLE [BBPrestamo] DROP CONSTRAINT [BBPrestamo_FK_3];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'BBPrestamo')
BEGIN
	 DECLARE @reftable_19 nvarchar(60), @constraintname_19 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'BBPrestamo'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_19, @constraintname_19
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_19+' drop constraint '+@constraintname_19)
	   FETCH NEXT from refcursor into @reftable_19, @constraintname_19
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [BBPrestamo]
END


CREATE TABLE [BBPrestamo]
(
	[id] INT  NOT NULL IDENTITY,
	[idTipoPrestamo] INT  NOT NULL,
	[IdPersona] INT  NOT NULL,
	[idLibro] INT  NOT NULL,
	[fechaGenera] DATETIME  NOT NULL,
	[fechaEntrega] DATETIME  NULL,
	[fechaRealDevolucion] DATETIME  NULL,
	[Observacion] VARCHAR(300)  NULL,
	[usuario] VARCHAR(50)  NOT NULL,
	[fechaVencimientoPrestamo] DATETIME  NOT NULL,
	CONSTRAINT [BBPrestamo_PK] PRIMARY KEY ([id])
);

BEGIN
ALTER TABLE [BBPrestamo] ADD CONSTRAINT [BBPrestamo_FK_1] FOREIGN KEY ([idTipoPrestamo]) REFERENCES [BBTipoPrestamo] ([idTipoPrestamo]) ON DELETE CASCADE
END
;

BEGIN
ALTER TABLE [BBPrestamo] ADD CONSTRAINT [BBPrestamo_FK_2] FOREIGN KEY ([IdPersona]) REFERENCES [BBPersona] ([id]) ON DELETE CASCADE
END
;

BEGIN
ALTER TABLE [BBPrestamo] ADD CONSTRAINT [BBPrestamo_FK_3] FOREIGN KEY ([idLibro]) REFERENCES [CTLibro] ([idLibro]) ON DELETE CASCADE
END
;

/* ---------------------------------------------------------------------- */
/* BBTipoPrestamo											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'BBTipoPrestamo')
BEGIN
	 DECLARE @reftable_20 nvarchar(60), @constraintname_20 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'BBTipoPrestamo'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_20, @constraintname_20
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_20+' drop constraint '+@constraintname_20)
	   FETCH NEXT from refcursor into @reftable_20, @constraintname_20
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [BBTipoPrestamo]
END


CREATE TABLE [BBTipoPrestamo]
(
	[idTipoPrestamo] INT  NOT NULL IDENTITY,
	[tipoPrestamo] VARCHAR(50)  NOT NULL,
	CONSTRAINT [BBTipoPrestamo_PK] PRIMARY KEY ([idTipoPrestamo])
);

/* ---------------------------------------------------------------------- */
/* BBLibroTipoPrestamo											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='BBLibroTipoPrestamo_FK_1')
	ALTER TABLE [BBLibroTipoPrestamo] DROP CONSTRAINT [BBLibroTipoPrestamo_FK_1];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='BBLibroTipoPrestamo_FK_2')
	ALTER TABLE [BBLibroTipoPrestamo] DROP CONSTRAINT [BBLibroTipoPrestamo_FK_2];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'BBLibroTipoPrestamo')
BEGIN
	 DECLARE @reftable_21 nvarchar(60), @constraintname_21 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'BBLibroTipoPrestamo'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_21, @constraintname_21
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_21+' drop constraint '+@constraintname_21)
	   FETCH NEXT from refcursor into @reftable_21, @constraintname_21
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [BBLibroTipoPrestamo]
END


CREATE TABLE [BBLibroTipoPrestamo]
(
	[idLibro] INT  NOT NULL,
	[idTipoPrestamo] INT  NOT NULL,
	CONSTRAINT [BBLibroTipoPrestamo_PK] PRIMARY KEY ([idLibro],[idTipoPrestamo])
);

BEGIN
ALTER TABLE [BBLibroTipoPrestamo] ADD CONSTRAINT [BBLibroTipoPrestamo_FK_1] FOREIGN KEY ([idLibro]) REFERENCES [CTLibro] ([idLibro]) ON DELETE CASCADE
END
;

BEGIN
ALTER TABLE [BBLibroTipoPrestamo] ADD CONSTRAINT [BBLibroTipoPrestamo_FK_2] FOREIGN KEY ([idTipoPrestamo]) REFERENCES [BBTipoPrestamo] ([idTipoPrestamo]) ON DELETE CASCADE
END
;

/* ---------------------------------------------------------------------- */
/* BBTipoDocumento											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'BBTipoDocumento')
BEGIN
	 DECLARE @reftable_22 nvarchar(60), @constraintname_22 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'BBTipoDocumento'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_22, @constraintname_22
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_22+' drop constraint '+@constraintname_22)
	   FETCH NEXT from refcursor into @reftable_22, @constraintname_22
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [BBTipoDocumento]
END


CREATE TABLE [BBTipoDocumento]
(
	[idTipoDocumento] INT  NOT NULL IDENTITY,
	[descripcionTipoDocumento] VARCHAR(50)  NULL,
	CONSTRAINT [BBTipoDocumento_PK] PRIMARY KEY ([idTipoDocumento])
);

/* ---------------------------------------------------------------------- */
/* BBPersona											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='BBPersona_FK_1')
	ALTER TABLE [BBPersona] DROP CONSTRAINT [BBPersona_FK_1];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='BBPersona_FK_2')
	ALTER TABLE [BBPersona] DROP CONSTRAINT [BBPersona_FK_2];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'BBPersona')
BEGIN
	 DECLARE @reftable_23 nvarchar(60), @constraintname_23 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'BBPersona'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_23, @constraintname_23
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_23+' drop constraint '+@constraintname_23)
	   FETCH NEXT from refcursor into @reftable_23, @constraintname_23
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [BBPersona]
END


CREATE TABLE [BBPersona]
(
	[id] INT  NOT NULL IDENTITY,
	[ApellidoPersona] VARCHAR(50)  NULL,
	[NombresPersona] VARCHAR(50)  NULL,
	[idTipoDocumento] SMALLINT  NULL,
	[nroDocumentoPersona] NUMERIC  NULL,
	[CuitPersona] VARCHAR(13)  NULL,
	[SexoPersona] VARCHAR(1)  NULL,
	[fechaNacimientoPersona] DATETIME  NULL,
	[estadoCivilPersona] INT  NULL,
	[MailPersona] VARCHAR(50)  NULL,
	[fechaBajaPersona] DATETIME  NULL,
	[fotoPath] VARCHAR(50)  NULL,
	[esDePasaje] INT  NULL,
	CONSTRAINT [BBPersona_PK] PRIMARY KEY ([id])
);

BEGIN
ALTER TABLE [BBPersona] ADD CONSTRAINT [BBPersona_FK_1] FOREIGN KEY ([idTipoDocumento]) REFERENCES [BBTipoDocumento] ([idTipoDocumento])
END
;

BEGIN
ALTER TABLE [BBPersona] ADD CONSTRAINT [BBPersona_FK_2] FOREIGN KEY ([estadoCivilPersona]) REFERENCES [BbEstadosCiviles] ([id])
END
;

/* ---------------------------------------------------------------------- */
/* BBDireccion											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='BBDireccion_FK_1')
	ALTER TABLE [BBDireccion] DROP CONSTRAINT [BBDireccion_FK_1];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='BBDireccion_FK_2')
	ALTER TABLE [BBDireccion] DROP CONSTRAINT [BBDireccion_FK_2];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'BBDireccion')
BEGIN
	 DECLARE @reftable_24 nvarchar(60), @constraintname_24 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'BBDireccion'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_24, @constraintname_24
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_24+' drop constraint '+@constraintname_24)
	   FETCH NEXT from refcursor into @reftable_24, @constraintname_24
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [BBDireccion]
END


CREATE TABLE [BBDireccion]
(
	[idDireccion] INT  NOT NULL IDENTITY,
	[idPersonaDireccion] INT  NOT NULL,
	[idTipoDireccion] SMALLINT  NOT NULL,
	[calleDireccion] VARCHAR(50)  NULL,
	[alturaDireccion] VARCHAR(50)  NULL,
	[pisoDireccion] VARCHAR(2)  NULL,
	[deptoDireccion] VARCHAR(10)  NULL,
	[idLocalidadDireccion] SMALLINT  NULL,
	[fechaBajaDireccion] DATETIME  NULL,
	CONSTRAINT [BBDireccion_PK] PRIMARY KEY ([idDireccion])
);

BEGIN
ALTER TABLE [BBDireccion] ADD CONSTRAINT [BBDireccion_FK_1] FOREIGN KEY ([idPersonaDireccion]) REFERENCES [BBPersona] ([id])
END
;

BEGIN
ALTER TABLE [BBDireccion] ADD CONSTRAINT [BBDireccion_FK_2] FOREIGN KEY ([idTipoDireccion]) REFERENCES [BBTipoDireccion] ([TiDe_Codigo])
END
;

/* ---------------------------------------------------------------------- */
/* BBTipoDireccion											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'BBTipoDireccion')
BEGIN
	 DECLARE @reftable_25 nvarchar(60), @constraintname_25 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'BBTipoDireccion'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_25, @constraintname_25
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_25+' drop constraint '+@constraintname_25)
	   FETCH NEXT from refcursor into @reftable_25, @constraintname_25
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [BBTipoDireccion]
END


CREATE TABLE [BBTipoDireccion]
(
	[TiDe_Codigo] SMALLINT  NOT NULL,
	[TiDe_Descripcion] VARCHAR(50)  NOT NULL,
	CONSTRAINT [BBTipoDireccion_PK] PRIMARY KEY ([TiDe_Codigo])
);

/* ---------------------------------------------------------------------- */
/* BBPersonaEstado											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='BBPersonaEstado_FK_1')
	ALTER TABLE [BBPersonaEstado] DROP CONSTRAINT [BBPersonaEstado_FK_1];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='BBPersonaEstado_FK_2')
	ALTER TABLE [BBPersonaEstado] DROP CONSTRAINT [BBPersonaEstado_FK_2];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'BBPersonaEstado')
BEGIN
	 DECLARE @reftable_26 nvarchar(60), @constraintname_26 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'BBPersonaEstado'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_26, @constraintname_26
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_26+' drop constraint '+@constraintname_26)
	   FETCH NEXT from refcursor into @reftable_26, @constraintname_26
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [BBPersonaEstado]
END


CREATE TABLE [BBPersonaEstado]
(
	[IdPersona] INT  NOT NULL,
	[IdEstado] INT  NOT NULL,
	[fechaDesde] DATETIME  NOT NULL,
	[fechaHasta] DATETIME  NULL,
	CONSTRAINT [BBPersonaEstado_PK] PRIMARY KEY ([IdPersona],[IdEstado],[fechaDesde])
);

BEGIN
ALTER TABLE [BBPersonaEstado] ADD CONSTRAINT [BBPersonaEstado_FK_1] FOREIGN KEY ([IdPersona]) REFERENCES [BBPersona] ([id]) ON DELETE CASCADE
END
;

BEGIN
ALTER TABLE [BBPersonaEstado] ADD CONSTRAINT [BBPersonaEstado_FK_2] FOREIGN KEY ([IdEstado]) REFERENCES [BBEstados] ([IdEstado]) ON DELETE CASCADE
END
;

/* ---------------------------------------------------------------------- */
/* BBEstados											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'BBEstados')
BEGIN
	 DECLARE @reftable_27 nvarchar(60), @constraintname_27 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'BBEstados'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_27, @constraintname_27
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_27+' drop constraint '+@constraintname_27)
	   FETCH NEXT from refcursor into @reftable_27, @constraintname_27
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [BBEstados]
END


CREATE TABLE [BBEstados]
(
	[IdEstado] INT  NOT NULL,
	[descripcionEstado] VARCHAR(50)  NOT NULL,
	[permitePrestamoDomiciliario] INT  NULL,
	CONSTRAINT [BBEstados_PK] PRIMARY KEY ([IdEstado])
);

/* ---------------------------------------------------------------------- */
/* BBPersonaTipoPersona											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='BBPersonaTipoPersona_FK_1')
	ALTER TABLE [BBPersonaTipoPersona] DROP CONSTRAINT [BBPersonaTipoPersona_FK_1];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='BBPersonaTipoPersona_FK_2')
	ALTER TABLE [BBPersonaTipoPersona] DROP CONSTRAINT [BBPersonaTipoPersona_FK_2];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'BBPersonaTipoPersona')
BEGIN
	 DECLARE @reftable_28 nvarchar(60), @constraintname_28 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'BBPersonaTipoPersona'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_28, @constraintname_28
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_28+' drop constraint '+@constraintname_28)
	   FETCH NEXT from refcursor into @reftable_28, @constraintname_28
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [BBPersonaTipoPersona]
END


CREATE TABLE [BBPersonaTipoPersona]
(
	[IdPersona] INT  NOT NULL,
	[idTipoPersona] INT  NOT NULL,
	[fechaDesde] DATETIME  NOT NULL,
	[fechaHasta] DATETIME  NULL,
	CONSTRAINT [BBPersonaTipoPersona_PK] PRIMARY KEY ([IdPersona],[idTipoPersona],[fechaDesde])
);

BEGIN
ALTER TABLE [BBPersonaTipoPersona] ADD CONSTRAINT [BBPersonaTipoPersona_FK_1] FOREIGN KEY ([IdPersona]) REFERENCES [BBPersona] ([id]) ON DELETE CASCADE
END
;

BEGIN
ALTER TABLE [BBPersonaTipoPersona] ADD CONSTRAINT [BBPersonaTipoPersona_FK_2] FOREIGN KEY ([idTipoPersona]) REFERENCES [BBTipoPersona] ([idTipoPersona]) ON DELETE CASCADE
END
;

/* ---------------------------------------------------------------------- */
/* BBTipoPersona											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'BBTipoPersona')
BEGIN
	 DECLARE @reftable_29 nvarchar(60), @constraintname_29 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'BBTipoPersona'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_29, @constraintname_29
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_29+' drop constraint '+@constraintname_29)
	   FETCH NEXT from refcursor into @reftable_29, @constraintname_29
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [BBTipoPersona]
END


CREATE TABLE [BBTipoPersona]
(
	[idTipoPersona] INT  NOT NULL,
	[descripcionTipoPersona] VARCHAR(50)  NULL,
	CONSTRAINT [BBTipoPersona_PK] PRIMARY KEY ([idTipoPersona])
);

/* ---------------------------------------------------------------------- */
/* BBTipoPersonaRegla											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='BBTipoPersonaRegla_FK_1')
	ALTER TABLE [BBTipoPersonaRegla] DROP CONSTRAINT [BBTipoPersonaRegla_FK_1];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='BBTipoPersonaRegla_FK_2')
	ALTER TABLE [BBTipoPersonaRegla] DROP CONSTRAINT [BBTipoPersonaRegla_FK_2];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'BBTipoPersonaRegla')
BEGIN
	 DECLARE @reftable_30 nvarchar(60), @constraintname_30 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'BBTipoPersonaRegla'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_30, @constraintname_30
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_30+' drop constraint '+@constraintname_30)
	   FETCH NEXT from refcursor into @reftable_30, @constraintname_30
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [BBTipoPersonaRegla]
END


CREATE TABLE [BBTipoPersonaRegla]
(
	[idTipoPersona] INT  NOT NULL,
	[idRegla] INT  NOT NULL,
	CONSTRAINT [BBTipoPersonaRegla_PK] PRIMARY KEY ([idTipoPersona],[idRegla])
);

BEGIN
ALTER TABLE [BBTipoPersonaRegla] ADD CONSTRAINT [BBTipoPersonaRegla_FK_1] FOREIGN KEY ([idTipoPersona]) REFERENCES [BBTipoPersona] ([idTipoPersona]) ON DELETE CASCADE
END
;

BEGIN
ALTER TABLE [BBTipoPersonaRegla] ADD CONSTRAINT [BBTipoPersonaRegla_FK_2] FOREIGN KEY ([idRegla]) REFERENCES [BBRegla] ([idRegla]) ON DELETE CASCADE
END
;

/* ---------------------------------------------------------------------- */
/* BBRegla											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='BBRegla_FK_1')
	ALTER TABLE [BBRegla] DROP CONSTRAINT [BBRegla_FK_1];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'BBRegla')
BEGIN
	 DECLARE @reftable_31 nvarchar(60), @constraintname_31 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'BBRegla'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_31, @constraintname_31
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_31+' drop constraint '+@constraintname_31)
	   FETCH NEXT from refcursor into @reftable_31, @constraintname_31
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [BBRegla]
END


CREATE TABLE [BBRegla]
(
	[idRegla] INT  NOT NULL IDENTITY,
	[reglaNombre] VARCHAR(100)  NOT NULL,
	[funcion] VARCHAR(100)  NOT NULL,
	[idTipoRegla] INT  NOT NULL,
	[parametros] VARCHAR(100)  NULL,
	CONSTRAINT [BBRegla_PK] PRIMARY KEY ([idRegla])
);

BEGIN
ALTER TABLE [BBRegla] ADD CONSTRAINT [BBRegla_FK_1] FOREIGN KEY ([idTipoRegla]) REFERENCES [BBTipoRegla] ([idTipoRegla])
END
;

/* ---------------------------------------------------------------------- */
/* BBTipoRegla											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'BBTipoRegla')
BEGIN
	 DECLARE @reftable_32 nvarchar(60), @constraintname_32 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'BBTipoRegla'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_32, @constraintname_32
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_32+' drop constraint '+@constraintname_32)
	   FETCH NEXT from refcursor into @reftable_32, @constraintname_32
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [BBTipoRegla]
END


CREATE TABLE [BBTipoRegla]
(
	[idTipoRegla] INT  NOT NULL,
	[tipoRegla] VARCHAR(30)  NOT NULL,
	CONSTRAINT [BBTipoRegla_PK] PRIMARY KEY ([idTipoRegla])
);

/* ---------------------------------------------------------------------- */
/* BBPrestamoRegla											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='BBPrestamoRegla_FK_1')
	ALTER TABLE [BBPrestamoRegla] DROP CONSTRAINT [BBPrestamoRegla_FK_1];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='BBPrestamoRegla_FK_2')
	ALTER TABLE [BBPrestamoRegla] DROP CONSTRAINT [BBPrestamoRegla_FK_2];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'BBPrestamoRegla')
BEGIN
	 DECLARE @reftable_33 nvarchar(60), @constraintname_33 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'BBPrestamoRegla'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_33, @constraintname_33
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_33+' drop constraint '+@constraintname_33)
	   FETCH NEXT from refcursor into @reftable_33, @constraintname_33
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [BBPrestamoRegla]
END


CREATE TABLE [BBPrestamoRegla]
(
	[idPrestamo] INT  NOT NULL,
	[idRegla] INT  NOT NULL,
	CONSTRAINT [BBPrestamoRegla_PK] PRIMARY KEY ([idPrestamo],[idRegla])
);

BEGIN
ALTER TABLE [BBPrestamoRegla] ADD CONSTRAINT [BBPrestamoRegla_FK_1] FOREIGN KEY ([idPrestamo]) REFERENCES [BBPrestamo] ([id]) ON DELETE CASCADE
END
;

BEGIN
ALTER TABLE [BBPrestamoRegla] ADD CONSTRAINT [BBPrestamoRegla_FK_2] FOREIGN KEY ([idRegla]) REFERENCES [BBRegla] ([idRegla]) ON DELETE CASCADE
END
;

/* ---------------------------------------------------------------------- */
/* BBPenalizacion											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='BBPenalizacion_FK_1')
	ALTER TABLE [BBPenalizacion] DROP CONSTRAINT [BBPenalizacion_FK_1];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='BBPenalizacion_FK_2')
	ALTER TABLE [BBPenalizacion] DROP CONSTRAINT [BBPenalizacion_FK_2];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='BBPenalizacion_FK_3')
	ALTER TABLE [BBPenalizacion] DROP CONSTRAINT [BBPenalizacion_FK_3];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'BBPenalizacion')
BEGIN
	 DECLARE @reftable_34 nvarchar(60), @constraintname_34 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'BBPenalizacion'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_34, @constraintname_34
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_34+' drop constraint '+@constraintname_34)
	   FETCH NEXT from refcursor into @reftable_34, @constraintname_34
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [BBPenalizacion]
END


CREATE TABLE [BBPenalizacion]
(
	[idPenalizacion] INT  NOT NULL,
	[idTipoPenalizacion] INT  NOT NULL,
	[IdPersona] INT  NOT NULL,
	[idPrestamo] INT  NOT NULL,
	[observacion] VARCHAR(300)  NULL,
	[fechaDesde] DATETIME  NULL,
	[usuario] VARCHAR(50)  NOT NULL,
	CONSTRAINT [BBPenalizacion_PK] PRIMARY KEY ([idPenalizacion])
);

BEGIN
ALTER TABLE [BBPenalizacion] ADD CONSTRAINT [BBPenalizacion_FK_1] FOREIGN KEY ([idTipoPenalizacion]) REFERENCES [BBTipoPenalizacion] ([idTipoPenalizacion])
END
;

BEGIN
ALTER TABLE [BBPenalizacion] ADD CONSTRAINT [BBPenalizacion_FK_2] FOREIGN KEY ([IdPersona]) REFERENCES [BBPersona] ([id])
END
;

BEGIN
ALTER TABLE [BBPenalizacion] ADD CONSTRAINT [BBPenalizacion_FK_3] FOREIGN KEY ([idPrestamo]) REFERENCES [BBPrestamo] ([id])
END
;

/* ---------------------------------------------------------------------- */
/* BBTipoPenalizacion											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'BBTipoPenalizacion')
BEGIN
	 DECLARE @reftable_35 nvarchar(60), @constraintname_35 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'BBTipoPenalizacion'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_35, @constraintname_35
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_35+' drop constraint '+@constraintname_35)
	   FETCH NEXT from refcursor into @reftable_35, @constraintname_35
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [BBTipoPenalizacion]
END


CREATE TABLE [BBTipoPenalizacion]
(
	[idTipoPenalizacion] INT  NOT NULL,
	[tipoPenalizacion] VARCHAR(50)  NOT NULL,
	[id] INT  NOT NULL IDENTITY,
	CONSTRAINT [BBTipoPenalizacion_PK] PRIMARY KEY ([idTipoPenalizacion],[id])
);

/* ---------------------------------------------------------------------- */
/* BbEstadosCiviles											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'BbEstadosCiviles')
BEGIN
	 DECLARE @reftable_36 nvarchar(60), @constraintname_36 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'BbEstadosCiviles'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_36, @constraintname_36
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_36+' drop constraint '+@constraintname_36)
	   FETCH NEXT from refcursor into @reftable_36, @constraintname_36
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [BbEstadosCiviles]
END


CREATE TABLE [BbEstadosCiviles]
(
	[id] INT  NOT NULL IDENTITY,
	[descripcion] VARCHAR(50)  NOT NULL,
	CONSTRAINT [BbEstadosCiviles_PK] PRIMARY KEY ([id])
);

/* ---------------------------------------------------------------------- */
/* BbReservaLibro											*/
/* ---------------------------------------------------------------------- */


IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='BbReservaLibro_FK_1')
	ALTER TABLE [BbReservaLibro] DROP CONSTRAINT [BbReservaLibro_FK_1];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='BbReservaLibro_FK_2')
	ALTER TABLE [BbReservaLibro] DROP CONSTRAINT [BbReservaLibro_FK_2];

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'BbReservaLibro')
BEGIN
	 DECLARE @reftable_37 nvarchar(60), @constraintname_37 nvarchar(60)
	 DECLARE refcursor CURSOR FOR
	 select reftables.name tablename, cons.name constraintname
	  from sysobjects tables,
		   sysobjects reftables,
		   sysobjects cons,
		   sysreferences ref
	   where tables.id = ref.rkeyid
		 and cons.id = ref.constid
		 and reftables.id = ref.fkeyid
		 and tables.name = 'BbReservaLibro'
	 OPEN refcursor
	 FETCH NEXT from refcursor into @reftable_37, @constraintname_37
	 while @@FETCH_STATUS = 0
	 BEGIN
	   exec ('alter table '+@reftable_37+' drop constraint '+@constraintname_37)
	   FETCH NEXT from refcursor into @reftable_37, @constraintname_37
	 END
	 CLOSE refcursor
	 DEALLOCATE refcursor
	 DROP TABLE [BbReservaLibro]
END


CREATE TABLE [BbReservaLibro]
(
	[id] INT  NOT NULL IDENTITY,
	[idPersona] INT  NOT NULL,
	[idLibro] INT  NOT NULL,
	[fechaGenera] DATETIME  NOT NULL,
	[fechaVencimiento] DATETIME  NULL,
	[finalizado] INT default 0 NULL,
	CONSTRAINT [BbReservaLibro_PK] PRIMARY KEY ([id])
);

BEGIN
ALTER TABLE [BbReservaLibro] ADD CONSTRAINT [BbReservaLibro_FK_1] FOREIGN KEY ([idPersona]) REFERENCES [BBPersona] ([id]) ON DELETE CASCADE
END
;

BEGIN
ALTER TABLE [BbReservaLibro] ADD CONSTRAINT [BbReservaLibro_FK_2] FOREIGN KEY ([idLibro]) REFERENCES [CTLibro] ([idLibro]) ON DELETE CASCADE
END
;
